package edu.uwlax.cs.oayonlinestore.client.gui.stock;

import com.google.gwt.user.client.ui.Hyperlink;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.gui.Store;
import edu.uwlax.cs.oayonlinestore.client.utils.DekoTable;
import edu.uwlax.cs.oayonlinestore.client.utils.TitlePage;
import edu.uwlax.cs.oayonlinestore.vo.CategoryVO;

public class PageStockCategoryList extends TitlePage {

	private class CategoryTable extends DekoTable {

		public CategoryTable() {
			super();
			addTitle("Category");
		}

		public void addItem(CategoryVO cv) {
			String anchor = Store.getFrame().getStockSubcategories().getAnchor(
					cv.getId());
			addRow().add(new Hyperlink(cv.getTitle(), anchor));
		}

	}

	CategoryTable table;

	protected void create() {
		setTitle("Select Category");
		add(table = new CategoryTable());
	}

	public String getID() {
		return "CategoryList";
	}

	public void onShow() {
		OnlinestoreRService.Util.getInstance()
				.getEditableCategoriesForCurrentUser(getAsyncHandler());
	}

	protected void onDataArrived(Object o) {
		table.cleanExceptHeader();
		CategoryVO[] cats = (CategoryVO[]) o;
		for (int i = 0; i < cats.length; i++)
			table.addItem(cats[i]);

	}

}
